<template>
  <el-dialog
    v-el-drag-dialog
    title="处理确认"
    :visible.sync="visible"
    width="700px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :model="temp" :rules="rules" label-position="right" label-width="80px">
      <el-form-item label="责任机构:" prop="orgId">
        <el-tree-select
          v-model="temp.orgId"
          :clearable="false"
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择',
            loading: $loadingPlugin.getTreeData
          }"
          :tree-params=" {
            'expand-on-click-node': false,
            'highlight-current': true,
            'filterable': true,
            props:{
              value:'orgId',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
          @node-click="selectTreeChange"
        />
      </el-form-item>
      <el-form-item label="处理人:" prop="userId">
        <el-select v-model="temp.userId" :loading="$loadingPlugin.getOwnersList" filterable style="width: 100%;" placeholder="请选择">
          <el-option
            v-for="item in ownersList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理结果:" prop="marker" class="block" label-width="90px">
        <el-input v-model="temp.marker" type="textarea" :rows="3" maxlength="255" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryCorpAndCrmOrg, confirmUserList, queryDealConfirmComplaintOrder } from '@/api'
  import getTreeData from '@/utils/jsonTree'
  import { mapGetters } from 'vuex'

  export default {
    name: 'deal-confirm',
    data() {
      return {
        visible: false,
        treeData: [],
        ownersList: [],
        temp: {
          orgId: '',
          userId: '',
          marker: ''
        },
        rules: {
          userId: [{ required: true, message: '请选择处理人', trigger: 'change' }],
          marker: [{ required: true, message: '请填写处理结果', trigger: 'change' }]
        }
      }
    },
    computed: {
      ...mapGetters([
        'corpSettingData'
      ])
    },
    created() {
    },
    methods: {
      async open(temp) {
        await Promise.all([this.getTreeData(temp.orgCorpSN)])
        return new Promise(async resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            await this.getOwnersList(this.temp)
            this.temp = Object.assign(this.temp, temp)
            this.temp.userId = temp.userId ? temp.userId : ''
          }
        })
      },
      // 获取所属机构数据
      async getTreeData(corpSN) {
        const res = await queryCorpAndCrmOrg({ corpSN })
        this.orgList = res.data
        const data = this.orgList.map(item => ({
          label: item.orgName,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      // 选择所属机构
      selectTreeChange(value) {
        this.$set(this.temp, 'userId', '')
        this.getOwnersList(value)
      },
      // 查询处理人列表
      async getOwnersList(orgInfo) {
        const b = this.corpSettingData.sp1 >> 2 & 0x01
        const res = await confirmUserList({
          orgId: orgInfo.orgId,
          corpSN: orgInfo.corpSN,
          corpparm1: b
        })
        this.ownersList = res.data
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const org = this.orgList.find(item => item.orgId === this.temp.orgId)
          const user = this.ownersList.find(item => item.userId === this.temp.userId)
          const params = {
            complainId: this.temp.complainId,
            orgId: this.temp.orgId,
            orgSNNo: org.orgSNNo,
            orgName: org.orgName,
            userId: this.temp.userId,
            userName: user.userName,
            marker: this.temp.marker
          }
          this.submit(params)
        })
      },
      async submit(params) {
        await queryDealConfirmComplaintOrder(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '操作成功!',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          orgId: '',
          userId: '',
          marker: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  & /deep/ .el-form-item {
    width: 48%;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
</style>
